﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using SDT.ServiceSystem;
using System.Threading;
using SDT.Libraries.Utilities;
using SDT.DataService.Services;
using SDT.DataService;

namespace SDT.CrawlSystem.MediaSrv
{
   public class MException
    {
        #region Logging
        private static LogHelper Logger = new LogHelper(
          string.Format("{0}media.cache.log", Global.Instance.LogFile.Substring(0, Global.Instance.LogFile.LastIndexOf(Path.DirectorySeparatorChar) + 1)));

        internal static void FlushLog()
        {
            lock (Logger)
                Logger.Flush();
        }
        #endregion

       public void WebException(int LinkID, int CID, int PID, WebException wex)
       {
           switch (wex.Status)
           {
               case WebExceptionStatus.CacheEntryNotFound:
                   break;
               case WebExceptionStatus.ConnectFailure:
                   break;
               case WebExceptionStatus.ConnectionClosed:
                   break;
               case WebExceptionStatus.KeepAliveFailure:
                   break;
               case WebExceptionStatus.MessageLengthLimitExceeded:
                   break;
               case WebExceptionStatus.NameResolutionFailure:
                   (new LinkDownloadService()).ChangeStatus(LinkID, (byte) MDWorkUnit.Status.OFFLINE);
                   break;
               case WebExceptionStatus.Pending:
                   break;
               case WebExceptionStatus.PipelineFailure:
                   break;
               case WebExceptionStatus.ProtocolError:
                   (new LinkDownloadService()).ChangeStatus(LinkID, (byte)MDWorkUnit.Status.NOT_FOUND);
                   break;
               case WebExceptionStatus.ProxyNameResolutionFailure:
                   break;
               case WebExceptionStatus.ReceiveFailure:
                   break;
               case WebExceptionStatus.RequestCanceled:
                   break;
               case WebExceptionStatus.RequestProhibitedByCachePolicy:
                   break;
               case WebExceptionStatus.RequestProhibitedByProxy:
                   break;
               case WebExceptionStatus.SecureChannelFailure:
                   break;
               case WebExceptionStatus.SendFailure:
                   break;
               case WebExceptionStatus.ServerProtocolViolation:
                   break;
               case WebExceptionStatus.Success:
                   break;
               case WebExceptionStatus.Timeout:
                   break;
               case WebExceptionStatus.TrustFailure:
                   break;
               case WebExceptionStatus.UnknownError:
                   break;
               default:
                   (new LinkDownloadService()).ChangeStatus(LinkID, (byte)MDWorkUnit.Status.INVALID);
                   break;
           };

           lock (Logger)
               Logger.AddEntry(string.Format("EX WebException  LinkID : {0}- C{1}- P{2} ",
                   LinkID, CID, PID));

       }
    }
}
